package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--修改备课包大小
 * 
 */
@CrossOrigin
@RestController
public class LecturePrePackSizeUptController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @ApiOperation(value = "老师 ---备授课--修改备课包大小")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/size/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel req, RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        YxyLecturePrePack lecturePrePack = yxyLecturePrePackRepository
                .findByIdAndUserIdAndSchoolIdAndIsDeleteNot(req.packId, userId, schoolId, 1);
        Integer packSize = req.packSize;

        if (lecturePrePack != null) {
            // 减去大小
            if (req.status == 0) {
                lecturePrePack.setPackSize(lecturePrePack.getPackSize() - packSize);
            } else {
                lecturePrePack.setPackSize(lecturePrePack.getPackSize() + packSize);
            }

            yxyLecturePrePackRepository.save(lecturePrePack);
        }
        RespModel model = new RespModel();
        model.setMessage("修改大小成功");

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 备课包id
        public String packId;
        // 要修改的备课包大小
        public Integer packSize;
        // 0减去大小 1添加大小
        public Integer status;
    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
